
import { deleteRequest, error, getRequest, ok, postRequest, putRequest } from "../utils/myAxios.ts";
import { Classroom, Res } from "../types";

// 获取班级列表
export async function getClassroomList(depId?:string): Promise<Res<Array<Classroom>>> {
    let url = `/classroom?`;
    if(depId){
        url += `&departmentId=${depId}`;
    }
    const list = await getRequest<Array<Classroom>>(url);
    console.log('获取班级列表', list);
    if (list?.length >= 0) {
        return {
            code: 200,
            data: list,
            flag: true,
            msg: "ok"
        };
    } else {
        return {
            code: 400,
            data: [],
            flag: false,
            msg: "请求失败"
        };
    }
}

// 添加班级
export async function addClassroom(classroom: Classroom): Res<Classroom> {
    const r = await postRequest<Classroom>(`/classroom`, classroom);
    console.log('添加班级：', r);
    if (r.id) {
        return ok(r, '添加成功');
    } else {
        return error(r);
    }
}

// 更新班级信息
export async function updateClassroom(classroom: Classroom): Res<Classroom> {
    const r = await putRequest<Classroom>(`/classroom/${classroom.id}`, classroom);
    console.log('修改班级res：', r);
    if (r.id) {
        return ok(r, '修改成功');
    } else {
        return error(r);
    }
}

// 删除班级
export async function deleteClassroom(id: number): Res<Classroom> {
    const r = await deleteRequest<Classroom>(`/classroom/${id}`);
    console.log('删除班级res：', r);
    if (r.id) {
        return ok(r, '删除成功');
    } else {
        return error(r);
    }
}

//查询单个班级
export async function getClassroomById(id: number): Res<Classroom> {
    const r = await getRequest<Classroom>(`/classroom/${id}`);
    console.log('查询班级res：', r);
    if (r.id) {
        return ok(r, '查询成功');
    } else {
        return error(r);
    }
}